<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="活动名称">
            <a-input v-decorator="['activity_name']" placeholder="请输入活动名称" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" icon="export" @click="handleExport">导出Excel</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    />
  </a-card>
</template>

<script>
import * as Api from '@/api/store/activity'
import { STable } from '@/components'

// table表头
const columns = [
  {
    title: '编号',
    dataIndex: 'sign_no'
  },
  {
    title: '手机号',
    dataIndex: 'mobile'
  },
  {
    title: '姓名', 
    dataIndex: 'name'
  },
  {
    title: '报名时间',
    dataIndex: 'create_time'
  }
]

export default {
  name: 'ActivityIndex',
  components: {
    STable
  },
  data () {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // table表头
      columns,
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => response.data.list)
      }
    }
  },
  methods: {
    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    // 刷新列表
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 导出Excel
    handleExport () {
      this.isLoading = true
      Api.exportExcel(this.queryParam)
        .then(response => {
          console.log('导出成功:', response)
          const fileUrl  = response.data.fileUrl
          window.open(fileUrl,'_self')
          // 检查响应头确认是否为文件流
          this.isLoading = false
        })
        .catch(error => {
          console.error('导出失败:', error)
          this.$message.error('导出失败')
          this.isLoading = false
        })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
